<html>

<head>
  <script type="text/javascript">

  function testStylesheet() {
    var link = document.createElement('link');
    link.addEventListener('load', () => {
      var cssLoaded = window.getComputedStyle(document.body)
        .getPropertyValue('background-color') == 'rgb(0, 0, 255)';
      window.domAutomationController.send(cssLoaded);
    });
    link.addEventListener('error', () => {
      window.domAutomationController.send(false);
    });

    link.rel = 'stylesheet';
    link.href = 'subresources/style.css';
    document.getElementsByTagName('head')[0].appendChild(link)
  }

  function testScript() {
    // Fail if window.scriptExecuted is already set.
    if (window.scriptExecuted) {
      window.domAutomationController.send(false);
      return;
    }

    var script = document.createElement('script');
    script.addEventListener('load', () => {
      window.domAutomationController.send(window.scriptExecuted);
    });
    script.addEventListener('error', () => {
      window.domAutomationController.send(false);
    });

    script.src = 'subresources/script.js';
    document.body.appendChild(script);
  }

  function testImage() {
    var img = document.createElement('img');
    img.addEventListener('load', () => {
      window.domAutomationController.send(true);
    });
    img.addEventListener('error', () => {
      window.domAutomationController.send(false);
    });

    img.src = 'subresources/image.png';
    document.body.appendChild(img);
  }

  function testXHR() {
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'text';
    xhr.addEventListener('load', () => {
      var xhrLoaded = xhr.status == 200 && xhr.responseText == 'Hello World\n';
      window.domAutomationController.send(xhrLoaded);
    });
    xhr.addEventListener('error', () => {
      window.domAutomationController.send(false);
    });

    xhr.open('GET', 'subresources/xhr_target.txt');
    xhr.send();
  }

  function testMedia() {
    var audio = document.createElement('audio');
    audio.addEventListener('loadeddata', () => {
      window.domAutomationController.send(true);
    });
    audio.addEventListener('error', () => {
      window.domAutomationController.send(false);
    });

    audio.src = 'subresources/ping.mp3';
    document.body.appendChild(audio);
  }

  function testWebSocket(url) {
    var ws = new WebSocket(url);
    var message = 'hello world';

    ws.onopen = () => {
      ws.send(message);
    }
    ws.onmessage = (event) => {
      window.domAutomationController.send(message == event.data);
    }
    ws.onerror = () => {
      window.domAutomationController.send(false);
    }
  }
  </script>
</head>

<body>
  <!-- Test subframe. -->
  <iframe src="subresources/subframe.html"></iframe>
</body>

</html>
